var basePage = require('../../../common/BasePage');
Page({
  behaviors: [basePage],
  data: {
    checkMobile: false,
    checkwxAuth: true,
    navInfo: getApp().globalData.navInfo,
    /**
     * 商家中心导航数据
     *  */
    shop: {
      title: '商家中心',
      icons: '/assets/newIndex/services/services17.png',
      link: '../../store_center/center'
    },
    agent_level: "1",
    uid: "",
    /**天气数据 */
    weather: {
      /**当前天气 */
      realtime: {},
      /**7天内数据 */
      daysForecast: {}
    },
    showHead: false,
    showRefuse: false,
    refuse: '',
    /**常用功能 */
    commonArr: [
      // {
      //     title: '当日账单',
      //     icons: '/assets/others/danrizhangdan.png',
      //     link: '../../today_check/check'
      // }, {
      //     title: '售后订单',
      //     icons: '/assets/others/shouhou.png',
      //     link: '/pages/cls/complaint/List'
      // },
      {
        title: '优惠券',
        icons: '/assets/newIndex/services/services1.png',
        link: '../../coupon/coupon'
      },
      {
        title: '物流配送',
        icons: '/assets/newIndex/services/services2.png',
        link: '/pages/cls/list/TabList?reqUrl=/home/user/express/index&type=0'
      },
      {
        title: '物流支付',
        icons: '/assets/newIndex/services/services3.png',
        link: '/pages/cls/list/logistics'
      },
      {
        title: '鲜花图册',
        icons: '/assets/newIndex/services/services4.png',
        link: '/pages/album/album?state=1'
      },
      {
        title: '常购清单',
        icons: '/assets/newIndex/services/services5.png',
        link: '/pages/cls/often/List'
      },
      {
        title: '收货地址',
        icons: '/assets/newIndex/services/services6.png',
        link: '/pages/addr/AddrList'
      },
      {
        title: '我的收藏',
        icons: '/assets/newIndex/services/services7.png',
        link: '/pages/album/collection/collection'
      }, {
        title: '完善资料',
        icons: '/assets/newIndex/services/services8.png',
        link: '/pages/cls/updateInfo/Index?type=1'
      }, {
        title: '运费详查',
        icons: '/assets/newIndex/services/services9.png',
        link: '/pages/logistics/freight/freight'
      },
      // {
      //     title: '联系客服',
      //     icons: '/assets/others/lianxikefu.png',
      //     link: '../../contact_service/contact'
      // },
      {
        title: '新手指南',
        icons: '/assets/newIndex/services/services10.png',
        link: '/pages/article/guide/New'
      },
      // {
      //     title: '关于平台',
      //     icons: '/assets/others/guanyupingtai.png',
      //     link: '/pages/article/guide/New'
      // },
      {
        title: '帮助中心',
        icons: '/assets/newIndex/services/services11.png',
        link: '/pages/article/help_center/help_center?cate_id=4'
      },
      {
        title: '每日花价',
        icons: '/assets/newIndex/services/services12.png',
        link: '/pages/cls/price/List'
      },
      {
        title: '交易须知',
        icons: '/assets/newIndex/services/services13.png',
        link: '/pages/article/help_center/help_center?cate_id=3&title=交易须知'
      },
      // {
      //     title: '钱包充值',
      //     icons: '/assets/newIndex/services/services14.png',
      //     link: '/pages/profit/recharge/recharge'
      // },
      {
        title: '仓库地址',
        icons: '/assets/newIndex/services/services15.png',
        link: '/pages/addr/whList'
      },
      {
        title: '集货站',
        icons: '/assets/newIndex/services/services22.png',
        link: '/pages/addr/site/index'
      },
      // {
      //     title: '通联支付',
      //     icons: '/assets/others/shouhuodizhi.png',
      //     link: '/pages/addr/whList'
      // },
      // {
      //     title: '我的名片',
      //     icons: '/assets/others/QRcode.png',
      //     link: '/pages/card/index'
      // },
      // {
      //     title: '我的推广',
      //     icons: '/assets/others/extension.png',
      //     link: '/pages/extension/index'
      // },
      // {
      //     title: '我的收益',
      //     icons: '/assets/others/profit.png',
      //     link: '/pages/profit/index'
      // },
    ],
    other_item: [
      // {
      //     title: '商家登陆',
      //     icons: '/assets/yuangong.png',
      //     link: '/pages/business/login/index?state=1'
      // },
      {
        title: '关于平台',
        icons: '/assets/newIndex/services/services16.png',
        link: '/pages/article/guide/New'
      },
      {
        title: '业务中心',
        icons: '/assets/newIndex/services/services18.png',
        link: '/pages/cls/staff/index/Index'
      },
      {
        title: '源花推广员',
        icons: '/assets/newIndex/services/services19.png',
        link: '/pages/profit/catalogue/Index'
      },

    ], //其他功能列表数组
  },

  onLoad: function (opt) {
    console.log("携带的参数", opt)
    if (opt.uid) {
      this.setData({
        uid: opt.uid
      })
    }
    this.getStatistics()
    //获取天气数据
    this.request({
      url: this.data.host + "/home/index/new_weather"
    }, res => {
      this.setData({
        weather: {
          daysForecast: res.data['dayWeathers'],
          realtime: res.data.now
        }
      })
    }, null, this.check_kf);
    // this.request({
    //     url: this.data.host + "/home/index/weather"
    // }, res => {
    //     // console.log("天气数据", res);
    //     this.setData({
    //         weather: {
    //             daysForecast: res.data['7daysForecast'],
    //             realtime: res.data.realtime
    //         }
    //     })
    // }, null, this.check_kf);
  },
  //统计用户访问量
  getStatistics() {
    this.request({
      url: this.data.host + "/home/index/view"
    }, res => {})
  },
  /**检查客服信息 */
  check_kf() {
    //客服电话
    let config1 = getApp().globalData.config1;
    if (config1 && config1.mobile) {
      // this.setData({
      //     mobile: config1.mobile
      // })
    } else {
      this.request({
        url: this.data.host + '/home/index/config?type=1'
      }, res => {
        getApp().globalData.config1 = res.data;
        // this.setData({
        //     mobile: res.data.mobile
        // })
      });
    }
  },
  onShow: function () {
    this.data.refuse = getApp().globalData.refuse;
    let that = this;
    this.request({
      url: this.data.host + "/home/user/info"
    }, res => {
      // console.log("user/info",res.data);
      let userInfo = getApp().globalData.userInfo = res.data;
      res.data.thumb = this.get_thumb(res.data.face);
      this.setData({
        userInfo
      });
      //完善用户信息
      if (!userInfo.audit) {
        wx.showModal({
          content: '请授权完善头像，昵称等信息',
          showCancel: true,
          title: '完善信息',
          success: (result) => {
            if (result.confirm) {
              wx.navigateTo({
                url: "/pages/cls/updateInfo/Index?type=2",
              })
              // wx.getUserProfile({
              //     desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
              //     success: (user) => {
              //         console.log("用户微信信息", user);
              //    that.request({
              //             url: that.data.host + '/home/user/audit',
              //             data: {
              //                 face: user.userInfo.avatarUrl,
              //                 nickname: user.userInfo.nickName,
              //                 sex: user.userInfo.gender
              //             },
              //             method: "POST",
              //         }, () => {
              //             that.onShow()
              //         })
              //     }
              // })
            }
          },
        })
      }
      if (res.data.show_shop == 0) {
        return
      }
      //重置导航
      let other_item = this.data.other_item;
      if (this.data.userInfo.is_warehouse == 1) { //1:分仓收益
        if (other_item.length > 4) {
          return;
        }
        other_item.unshift({
          title: '仓库收益',
          icons: '/assets/others/shengqingruzhu.png',
          // link: '/pages/profit/index?type=3&from=wh_money'
          link: '/pages/profit/profitWh/index'
        });

      } else {
        if (other_item.length > 3) {
          return;
        }
      }

      //clschen 2022.01.02 添加商家中心，提示跳转小程序
      other_item.unshift(this.data.shop);
      // other_item.unshift({
      //     title: '申请入驻',
      //     icons: '/assets/others/shengqingruzhu.png',
      //     link: '/pages/cls/applyShop/Index'
      // });
      if (res.data.supplier_verify == 1) {
        this.data.refuse = getApp().globalData.refuse = "";
        // 
      } else {
        other_item.unshift({
          title: '申请入驻',
          icons: '/assets/others/shengqingruzhu.png',
          link: '/pages/cls/applyShop/Index'
        });

      }
      if (res.data.supplier_verify == 2) {
        if (!this.data.refuse) {
          this.req_refuse();
        } else {
          this.setData({
            showRefuse: true,
            refuse: '入驻被驳回:' + this.data.refuse
          })
        }
      }
      this.setData({
        other_item
      })
    });
  },
  login_reload() {
    let userInfo = this.data.userInfo;
    this.setData({
      userInfo
    });
    this.onShow();

  },
  /**
   * 其他功能点击事件
   * @param {Event} e 
   */
  order_click(e) {
    // console.log("order_click", e);
    let index = e.currentTarget.dataset.index;
    let item = this.data[e.currentTarget.dataset.arr][index];
    console.log('item.title', item.title)
    console.log('item.title', item.link)
    if ('业务中心' == item.title) {
      return wx.navigateToMiniProgram({
        appId: 'wxd2a5d96762875cf9',
        path: '/pages/cls/staff/index/Index',
        extraData: {
          foo: 'bar'
        },
        envVersion: 'release',
        success(res) {
          // 打开成功
        }
      })
    }
    if ('申请入驻' == item.title) {
      ///**0:待审核,1:通过认证,2:认证被拒绝,3未提交 */
      if (!this.data.userInfo.supplier_verify) {
        return wx.showToast({
          title: '你的申请正在审核中，请耐心等待',
          icon: 'none'
        })
      }
    }
    if ('源花推广员' == item.title) {
      if (this.data.userInfo.is_agent == 0) { //是否为代理商[0:否,1:是]
        this.setData({
          showHead: true
        })
        return;
      }
    }
    if ('商家中心' == item.title) {
      return wx.navigateToMiniProgram({
        appId: 'wx6d1b6d0a8eae12d7',
        path: '/pages/store_center/center',
        extraData: {
          foo: 'bar'
        },
        envVersion: 'release',
        success(res) {
          // 打开成功
        }
      })
    }
    if (item.title == "通联支付") {
      //已删除
      this.toBank()
      return;
    }


    wx.navigateTo({
      url: item.link,
    })
  },
  order_click2() {
    wx.navigateTo({
      url: '/pages/region/regionCenter/index',
    })
  },
  // 关闭团长说明
  onClose() {
    this.setData({
      showHead: false
    })
  },
  /**
   * 获取拒绝原因
   */
  req_refuse() {
    this.request({
      url: this.data.host + "/home/supplier/combine/detail",
    }, res => {
      // console.log("/home/supplier/combine/detail", res);
      this.data.refuse = getApp().globalData.refuse = res.data.refuse;
      wx.showModal({
        cancelText: '暂不处理',
        confirmText: '重新申请',
        title: '申请入驻被拒绝',
        content: res.data.refuse,
        success(obj) {
          if (obj.confirm) {
            wx.navigateTo({
              url: '../../cls/applyShop/Index',
            })
          }
        }
      })
    });
  },
  /**
   * 跳转天气页面
   */
  weather_click() {
    // console.log("weather_click");
    let that = this;
    wx.navigateTo({
      url: '/pages/album/weather/weather',
      success: function (obj) {
        //传递产品数据
        obj.eventChannel.emit('weather', that.data.weather.daysForecast)
      }
    })
  },

  /* 
   跳转提现
   */
  wallet_click() {
    wx.navigateTo({
      url: '/pages/profit/walletList/index',
    })
    // wx.navigateTo({
    //     url: "/pages/profit/wallet/wallet?type=2"
    // })
  },

  /* 
	 申请团长
	 */
  submit_click() {
    this.request({
      url: this.data.host + "/home/user/agent/apply",
      data: {
        agent_level: this.data.agent_level
      },
      method: "post"
    }, res => {
      wx.showToast({
        title: res.msg,
        icon: "none"
      })
      this.setData({
        showHead: false
      })
    });
  },
  onChange(e) {
    this.setData({
      agent_level: e.detail
    })
  },
  onOrder(e) {
    console.log("跳转订单", e)
    var url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: url,
    })
  },

  toMeans() {
    wx.navigateTo({
      url: "/pages/cls/updateInfo/Index?type=2",
    })
  },
  //跳转至通联支付
  toBank() {
    var data = ""
    var supplier = this.data.userInfo
    var examine = wx.getStorageSync('examine') //1企业信息审核中2影印件审核中
    console.log("examine", wx.getStorageSync('examine'))
    if (supplier.allin_is_protocol == 1) {
      // if(supplier.allin_bank==0){
      // 	//重新绑定银行卡
      // 	wx.navigateTo({
      // 		url: '/pages/unionPay/binding/index',
      // 	})
      // 	return;
      // }
      wx.navigateTo({
        url: '/pages/unionPay/unionInfo/index?allintype=' + supplier.allin_type,
        success: function (res) {
          // 通过eventChannel向被打开页面传送数据
          res.eventChannel.emit('supplierInfo', {
            data: supplier
          })
        }
      })
      return;
    }
    if (supplier.allin_type) {
      //2企业3个人
      data = "?userType=" + supplier.allin_type
      if (supplier.allin_mobile_verify == 0) {
        //未绑定手机号
        data = data + "&step=1"
      } else if ((supplier.allin_type == 3 && supplier.allin_verify == 0) || (supplier.allin_type == 2 && supplier.allin_info_verify == 0)) {
        //个人未实名认证或者企业未设置企业信息
        if (supplier.allin_type == 2 && examine == 1) {
          wx.showToast({
            title: '企业信息审核中，请稍后刷新重试',
            icon: "none"
          })
          return;
        }
        data = data + "&step=2"
      } else if (supplier.allin_type == 2 && supplier.allin_bank == 0 && supplier.allin_info_verify == 2) {
        //个体企业认证通过，银行卡未绑定
        wx.showToast({
          title: supplier.allin_fail_reason ? supplier.allin_fail_reason : "银行卡绑定失败",
          icon: "none"
        })
        data = data + "&step=2&bankType=1"
      } else if (supplier.allin_type == 2 && supplier.allin_info_verify == 3) {
        //企业信息未通过
        wx.showToast({
          title: supplier.allin_fail_reason ? supplier.allin_fail_reason : "企业认证未通过",
          icon: "none"
        })
        data = data + "&step=2"
      } else if ((supplier.allin_type == 3 && supplier.allin_bank == 0) || (supplier.allin_type == 2 && (supplier.allin_account_verify == 0 || supplier.allin_id_verify == 0))) {
        //个人未绑定银行卡或者企业未上传影印件
        if (supplier.allin_type == 2 && examine == 2) {
          wx.showToast({
            title: '影印件审核中，请稍后刷新重试',
            icon: "none"
          })
          return;
        }
        data = data + "&step=3"
      } else if (supplier.allin_type == 2 && (supplier.allin_account_verify == 2 || supplier.allin_id_verify == 2)) {
        //企业影印件审核未通过
        wx.showToast({
          title: '影印件审核未通过',
          icon: "none"
        })
        data = data + "&step=3"
      } else if (supplier.allin_is_protocol == 0) {
        //未签约协议
        this.signing()
        wx.removeStorageSync('examine') //移除待审核缓存
        return;
      }
    }
    wx.removeStorageSync('examine') //移除待审核缓存
    setTimeout(() => {
      wx.navigateTo({
        url: "/pages/unionPay/addUser/index" + data,
      })
    }, 100)
  },
  //签约协议
  signing() {
    var url = "/home/allinpay/member/signContract"
    var data = {
      jump_type: 2,
      source: 1
    }
    this.request({
      url: this.data.host + url,
      method: "GET",
      data: data
    }, res => {
      // wx.showToast({
      //   title: res.msg,
      //   icon: "none"
      // })
      var targetUrl = res.data.auth_url
      wx.navigateToMiniProgram({
        appId: 'wxc46c6d2eed27ca0a', //通联小程序 appId
        path: 'pages/merchantAddress/merchantAddress', //通联小程序页面路径 
        extraData: {
          targetUrl: targetUrl //商户拼接的请求报文
        },
        envVersion: 'release', //develop 开发版 trial 体验版 release 正式版
        success(res) {
          // 打开成功
          console.log("跳转通联支付", res)
        }
      })

    })
  },
  toKefu() {
    wx.navigateTo({
      url: '../../contact_service/contact',
    })
  },
  toSettle() {
    ///**0:待审核,1:通过认证,2:认证被拒绝,3未提交 */
    if (this.data.userInfo.supplier_verify == 0) {
      return wx.showToast({
        title: '你的申请正在审核中，请耐心等待',
        icon: 'none'
      })
    } else if (this.data.userInfo.supplier_verify == 1) {
      wx.navigateToMiniProgram({
        appId: 'wx6d1b6d0a8eae12d7',
        path: '/pages/store_center/center',
        extraData: {
          foo: 'bar'
        },
        envVersion: 'release',
        success(res) {
          // 打开成功
        }
      })
    } else if (this.data.userInfo.supplier_verify == 2) {
      if (!this.data.refuse) {
        this.req_refuse();
      } else {
        this.setData({
          showRefuse: true,
          refuse: '入驻被驳回:' + this.data.refuse
        })
      }
    } else if (this.data.userInfo.supplier_verify == 3) {
      wx.navigateTo({
        url: '/pages/cls/applyShop/Index',
      })
    }
  },
  /**
   * 点击客服按钮
   */
  service_click() {
    // console.log("socket连接状态", getApp().globalData.showSocket)
    if (!getApp().globalData.showSocket) {
      wx.showModal({
        title: '客服系统未链接',
        content: '请确定是否链接',
        success(res) {
          if (res.confirm) {
            // console.log('用户点击确定')
            getApp().globalData.number = 0
            getApp().globalData.againSocket = 1
            getApp().connectSocket();

          } else if (res.cancel) {
            // console.log('用户点击取消')
          }
        }
      })
      return;
    }
    // console.log("聊天次数", getApp().globalData.number)
    if (getApp().globalData.number >= 3) {
      wx.showToast({
        title: '聊天服务异常，请重启小程序或者联系客服',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    let warehouse = getApp().globalData.warehouse;
    wx.navigateTo({
      url: '/pages/chat/chatList/chatList'
    })
  },
  onPhoneauth(e) {
    console.log("授权手机号", e)
    if (e == 0) {
      wx.showToast({
        title: '请授权手机号！',
        icon: "none"
      })
    } else {
      this.setData({
        showWxAuth: false
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '源花采购',
      path: 'pages/tabbar/my/my?uid=' + this.data.userInfo.uid,
      success: (res) => {
        console.log("转发成功", res);
      },
      fail: (res) => {
        console.log("转发失败", res);
      }
    }
  }
})